﻿using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Exams;
using YAPACS.BusinessObjects.Managements.WorkPlans;
using YAPACS.Organizations;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Register;

public class Candidate(IRepository<UserWorkPlan, Guid> userWorkPlanRepository, IRepository<Exam, Guid> examRepository,
    IRepository<UserDeviceWorkPlan, Guid> userDeviceWorkPlanRepository)
    : WorkflowBase, ICandidate, ITransientDependency
{
    private readonly IRepository<UserWorkPlan, Guid> _userWorkPlanRepository = userWorkPlanRepository;
    private readonly IRepository<UserDeviceWorkPlan, Guid> _userDeviceWorkPlanRepository = userDeviceWorkPlanRepository;
    private readonly IRepository<Exam, Guid> _examRepository = examRepository;

    protected async override Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        //return base.InvokeCoreAsync(input);
        //根据科室/日期获取  报告最少的医生
        const string key = "Exams";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is List<Exam> exams)
        {
            foreach (var exam in exams)
            {
                ApplicationUser user = null;
                var department = exam.Department;
                var startTime = exam.ExamAppointment == null ? Clock.Now.TimeOfDay : exam.ExamAppointment.StartTime.TimeOfDay;
                var endTime = exam.ExamAppointment == null ? Clock.Now.TimeOfDay : exam.ExamAppointment.EndTime.TimeOfDay;
                var date = exam.ExamAppointment == null ? Clock.Now.Date : exam.ExamAppointment.StartTime.Date;
                //根据设备获取人员
                var userDeviceWorkPlan = await _userDeviceWorkPlanRepository.FindAsync(e => e.Device == exam.ExamDevice && e.Department == department && e.Date == date && e.TimeInterval != null && e.TimeInterval.StartTime <= startTime && e.TimeInterval.EndTime > startTime);
                if (userDeviceWorkPlan != null && userDeviceWorkPlan.User != null)
                {
                    user = userDeviceWorkPlan.User;
                }
                if (user == null)
                {
                    Dictionary<ApplicationUser, int> candidates = [];
                    //获取人员排班
                    var userWorkPlans = await _userWorkPlanRepository.GetListAsync(e => e.Department == department && e.Date == date && e.TimeInterval != null && e.TimeInterval.StartTime <= startTime && e.TimeInterval.EndTime > startTime);
                    var users = userWorkPlans.Select(e => e.User).ToList();
                    foreach (var candidate in users)
                    {
                        var count = await _examRepository.CountAsync(e => e.ReportCandidate == candidate);
                        candidates.Add(candidate, count);
                    }
                    user = candidates.OrderBy(e => e.Value).ThenBy(e => e.Key.Name).Select(e => e.Key).FirstOrDefault();
                }
                exam.ReportCandidate = user;
            }
            return new WorkflowOutput
            {
                Success = true
            };
        }
        return new WorkflowOutput
        {
            Success = false,
            Info = "未找到数据"
        };
    }
}
